﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hangjing.Common;
using Hangjing.SQLServerDAL;
using Hangjing.Model;
using System.Web.Script.Serialization;
using System.Text;

/// <summary>
/// 
/// </summary>
public partial class qy_54tss_Admin_Shop_outStorage : System.Web.UI.Page
{
    StorageOrder dal = new StorageOrder();
    StorageLog dalpfl = new StorageLog();


    protected void Page_Load(object sender, EventArgs e)
    {
        ValidatorSet validator = new ValidatorSet("Admin");
        validator.SetValidator();

        if (!IsPostBack)
        {
            int SortId = HjNetHelper.GetQueryInt("id", 0);
            int cityid = HjNetHelper.GetQueryInt("cityid", 0);

            WebUtility.BindList("id", "classname", SectionProxyData.GetEdelivergroupList().Where(a => a.Status == cityid).ToList(), DDLArea);

            if (SortId > 0)
            {
                pageType.Text = "出库明细";
                BindFoodSortData(SortId);
                btSave.Visible = false;
                addbox.Visible = false;
            }
            else
            {
                pageType.Text = "物料出库";
                EAdminInfo admin = UserHelp.GetAdmin();
                if (cityid == 0 && admin.CityID > 0)
                {
                    Response.Redirect("outStorage.aspx?cityid=" + admin.CityID);
                }
            }

        }
    }

    protected void btSave_Click(object sender, EventArgs e)
    {
        Hangjing.Model.StorageOrderInfo info = new StorageOrderInfo();
        info.Id = HjNetHelper.GetQueryInt("id", 0);
        if (info.Id > 0)
        {
            info = dal.GetModel(info.Id);
        }
        else
        {
            info.adminname = UserHelp.GetAdmin().AdminName;
            info.addtime = DateTime.Now;
        }

        info.reveint1 = 0;
        info.sremark = WebUtility.InputText(tbsremark.Text);
        info.orderid = "";
        info.reveint2 = 0;
        info.stype = 1;
        info.reveint3 = 0;
        info.revevar1 = "";
        info.revevar2 = "";
        info.revevar3 = "";
        info.revedate1 = TimeHelper.getDefaultTime();
        info.revedate2 = 0;
        int cityid = HjNetHelper.GetQueryInt("cityid", 0);
        info.cityid = cityid;
        info.gid = WebUtility.InputText(DDLArea.SelectedValue, true);

        if (info.Id == 0)
        {
            int pid = dal.Add(info);
            if (pid > 0)
            {
                //添加物料记录，
                IList<StorageLogInfo> styles = new JavaScriptSerializer().Deserialize<IList<StorageLogInfo>>(this.hidStyle.Value);
                string sql = "";
                foreach (var item in styles)
                {
                    item.stype = 1;
                    item.orderid = "";
                    item.devid = 0;
                    item.remark = "";
                    item.adddate = DateTime.Now;
                    item.reveint1 = pid;
                    item.reveint2 = 0;
                    item.reveint3 = 0;
                 
                    item.revevar3 = "";

                    sql += " UPDATE dbo.Devices SET stock = stock - "+ item.changestock+" WHERE devNO = '"+item.revevar1+"';";

                }

                DataTable dt = CollectionHelper.ConvertTo(styles, "StorageLog");
                Hangjing.DBUtility.SQLHelper.SqlBulkCopyData(dt);
                //修改库存
                if (sql.Length > 0)
                {
                    WebUtility.excutesql(sql);
                }

                AlertScript.RegScript(this.Page, UpdatePanel1, "tipsWindown('提示','text:操作成功','250','150','true','','true','text');");
            }
            else
            {
                AlertScript.RegScript(this.Page, UpdatePanel1, "tipsWindown('提示','text:新增失败','250','150','true','','true','text');");
            }
        }
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    /// <param name="id"></param>
    protected void BindFoodSortData(int id)
    {
        StorageOrderInfo info = dal.GetModel(id);
        if (info != null)
        {
            tbsremark.Text = info.sremark;
            WebUtility.SelectValue(DDLArea, info.gid);
            IList<StorageLogInfo> foodlist = dalpfl.GetList(100, 1, "reveint1=" + id, "sId", 0);
            WebUtility.BindRepeater(rptfood, foodlist);
        }
    }
}
